#!/bin/bash
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# ====================================================
# Description:Aria2 一键脚本 for Debian 8+ 、CentOS 7、Ubuntu 16+
# Author:MOERATS
# Site:moerats.com
# ====================================================

#fonts color
Red="\033[31m" 
Font="\033[0m"
Blue="\033[36m"

#root permission
root(){
        if [[ $EUID -ne 0 ]]; then
        echo "${Red}Error:请使用root运行该脚本！"${Font} 1>&2
        exit 1
        fi
}

#check system
system(){
        if [ -f /etc/redhat-release ]; then
        release="centos"
    elif cat /etc/issue | grep -Eqi "debian"; then
        release="debian"
    elif cat /etc/issue | grep -Eqi "ubuntu"; then
        release="ubuntu"
    elif cat /etc/issue | grep -Eqi "centos|red hat|redhat"; then
        release="centos"
    elif cat /proc/version | grep -Eqi "debian"; then
        release="debian"
    elif cat /proc/version | grep -Eqi "ubuntu"; then
        release="ubuntu"
    elif cat /proc/version | grep -Eqi "centos|red hat|redhat"; then
        release="centos"
    fi
}

#check version
version(){
	if [[ -s /etc/redhat-release ]]; then
	 version=`cat /etc/redhat-release|sed -r 's/.* ([0-9]+)\..*/\1/'`
	else
	 version=`grep -oE  "[0-9.]+" /etc/issue | cut -d . -f 1`
	fi
    echo -e "${Blue}正在为系统进行环境检测...${Font} "
	for EXE in grep cut xargs systemctl ip awk
	do
		if ! type -p ${EXE}; then
			echo -e "${Red}系统精简厉害，脚本自动退出${Font}"
			exit 1
		fi
	done
    if [[ "${release}" = "centos" && ${version} -ge 7 ]];then
        echo -e "${Blue}当前系统为CentOS ${version}，且符合安装要求！${Font} "
    elif [[ "${release}" = "debian" && ${version} -ge 8 ]];then
        echo -e "${Blue}当前系统为Debian ${version}，且符合安装要求！${Font} "
    elif [[ "${release}" = "ubuntu" && `echo "${version}" | cut -d '.' -f1` -ge 16 ]];then
        echo -e "${Blue}当前系统为Ubuntu ${version}，且符合安装要求！${Font} "
    else
        echo -e "${Red}当前系统为不在支持的系统列表内，安装中断！${Font} "
        exit 1
    fi
}

#enter info
enter(){
    stty erase '^H' && read -p "请输入你的Aria2密钥:" pass
    [[ -z $pass ]] && echo -e "${Red}输入有误，请重新运行脚本! ${Font} "&& exit 1;
    stty erase '^H' && read -p "请输入你要自动转移的目录，且必须为绝对路径地址(如输入/www/movies):" mv_dir
    Dri="$(echo -n "$mv_dir" |awk -F '/' '{print $2}')"
    [[ -z $Dri ]] && echo -e "${Red}文件夹格式错误，安装退出，请重新运行本脚本并输入正确的路径！ "&& exit 1;
}

#install depend
depend(){
    if [ "${release}" = "centos" ]; then
        yum -y install make crontabs curl
        systemctl start crond
        systemctl enable crond
    else
        apt update -y
        apt-get install -y make cron curl
    fi
}

#install aria2
aria2(){
if [[ ! -d ${mv_dir} ]]; then
mkdir -p ${mv_dir}
fi
echo -e "${Blue}开始安装Aria2...${Font}"
bit=`uname -m`
if [[ ${bit} = "x86_64" ]]; then
	KernelBit="64"
else
	KernelBit="32"
fi
wget -N --no-check-certificate https://db.ci/aria2/aria2-1.34.0-linux-${KernelBit}.tar.gz
tar zxvf aria2-1.34.0-linux-${KernelBit}.tar.gz
rm -rf aria2-1.34.0-linux-${KernelBit}.tar.gz
cd aria2-1.34.0-linux-${KernelBit}
make install
 EXEC="$(command -v aria2c)"
    if [[ -n ${EXEC} ]]; then
        echo -e "${Blue}Aria2安装成功！${Font}"
    else
        echo -e "${Red}Aria2安装失败！${Font}"
        exit 1
    fi
cd ..
rm -rf aria2-1.34.0-linux-${bit}
mkdir "/root/.aria2" && mkdir /root/down
wget -N --no-check-certificate https://db.ci/aria2/dht.dat -P '/root/.aria2/'
wget -N --no-check-certificate https://db.ci/aria2/aria2.conf -P '/root/.aria2/'
wget -N --no-check-certificate https://db.ci/aria2/trackers-list-aria2.sh -P '/root/.aria2/'
wget -N --no-check-certificate https://db.ci/aria2/mv_dir.sh -P '/root/.aria2/'
wget -N --no-check-certificate https://db.ci/aria2/aria2.service -P '/etc/systemd/system/'
touch /root/.aria2/aria2.session
chmod +x /root/.aria2/trackers-list-aria2.sh /root/.aria2/mv_dir.sh
chmod 777 /root/.aria2/aria2.session 
echo -e "${Blue}开始设置trackers-list自动更新！${Font}"
echo "0 3 */7 * * /root/.aria2/trackers-list-aria2.sh
*/5 * * * * systemctl restart aria2" > /root/bt.cron
crontab bt.cron
rm -rf bt.cron
}

#open firewall
firewall(){
    if [ "${release}" = "centos" ]; then
        firewall-cmd --zone=public --add-port=6800/tcp --permanent
        firewall-cmd --reload
    fi
    sed -i "s#rpc-secret=#rpc-secret=$pass#g" /root/.aria2/aria2.conf
    sed -i "s#/root/movies#$mv_dir#g" /root/.aria2/mv_dir.sh
    systemctl start aria2
    systemctl enable aria2
}

#Complete info
info(){
        ip=`curl http://whatismyip.akamai.com`
        echo -e "———————————————————————————————————————"
        echo -e "${Blue}Aria2安装完成${Font}"
        echo -e "${Blue}下载目录：/root/down${Font}"
        echo -e "${Blue}转移目录：$mv_dir${Font}"
        echo -e "${Blue}连接ip：$ip${Font}"
        echo -e "${Blue}连接端口：6800${Font}"
        echo -e "${Blue}密码：${pass}${Font}"
        echo -e "${Blue}Aria2 Web端地址：http://aria2.yzm.pw${Font}"
        echo -e "———————————————————————————————————————"
}

root;
system;
version;
enter;
depend;
aria2;
firewall;
info